package com.novax.ex.activity.provider.service;

import com.alibaba.excel.EasyExcel;
import com.novax.ex.activity.infrastructure.mapper.RankStandingsMapper;
import com.novax.ex.activity.provider.api.model.ExcelRankStandings;
import com.novax.ex.activity.provider.handler.consumer.RankExcelListener;
import com.novax.ex.common.base.BaseMapper;
import com.novax.ex.common.base.BaseService;
import com.novax.ex.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import java.io.IOException;

/**
 * @Description
 * @Author ChenXi
 * @Date 2023/11/23/15:46
 */

@Service
@Slf4j
public class RankStandingsService extends BaseService {

    @Resource
    private RankStandingsMapper rankStandingsMapper;

    @Override
    protected BaseMapper getBaseMapper() {

        return rankStandingsMapper;
    }

    /**
     * @param excelFile
     * @param rankSubConfigId
     * @Description: 更新排名数据
     * @Author: ChenXi
     * @Date: 2023/11/24 11:55
     * @return: com.novax.ex.common.results.ReturnResult<?>
     **/
    public boolean updateByExcel(MultipartFile excelFile, Long rankSubConfigId) {
        // 更新数据前, 清空旧数据
        rankStandingsMapper.deleteByRankSubId(rankSubConfigId);

        try {
            EasyExcel.read(excelFile.getInputStream(), ExcelRankStandings.class, new RankExcelListener(rankSubConfigId, rankStandingsMapper))
                .sheet()
                .doRead();
        } catch (IOException e) {
            log.error("RankStandingsService updateByExcel error");
            throw new BusinessException( "RankStandingsService updateByExcel error");
        }

        return true;
    }


}


